package com.ssy.lingxi.purchase.config.rabbitmq.consumer;

import com.rabbitmq.client.Channel;
import com.ssy.lingxi.component.rabbitMQ.constants.MessageConstants;
import com.ssy.lingxi.purchase.api.dto.request.ExamineQuotedPriceBatchRequest;
import com.ssy.lingxi.purchase.config.rabbitmq.OrderRbbitMqRequest;
import com.ssy.lingxi.purchase.service.PurchaseInquiryService;
import com.ssy.lingxi.purchase.service.bidding.BiddingService;
import com.ssy.lingxi.purchase.serviceimpl.BaseBiddingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * 延时消息消费者
 */
@Slf4j
@Component
public class OrderRabbitMQConsumer extends BaseBiddingService {


    @Resource
    BiddingService biddingService;

    @Resource
    PurchaseInquiryService purchaseInquiryService;


    /**
     * 开始报名-待报名
     */
    @RabbitListener(queues = MessageConstants.PURCHASE_BIDDING_DELAY_QUEUE)
    public void cfgUserReceiveDealy(OrderRbbitMqRequest request, Message message, Channel channel) throws IOException {
        log.info("竞价报名结束- 开始处理:接收时间:{},接受内容:{}", System.currentTimeMillis(), request.toString());
        try {
            biddingService.examine(request, message, channel);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("竞价报名结束- 处理完成");
        } catch (Exception e) {
            log.error("============消费失败,尝试消息补发再次消费!==============");
            log.error(e.getMessage());
            channel.basicRecover(false);
        }

    }

    /**
     * 结束竞价
     */
    @RabbitListener(queues = MessageConstants.PURCHASE_END_BIDDING_DELAY_QUEUE)
    public void cfgUserStartReceiveDealyEnd(OrderRbbitMqRequest request, Message message, Channel channel) throws IOException {
        log.info("竞价截止- 开始处理:接收时间:{},接受内容:{}", System.currentTimeMillis(), request.toString());
        try {
            biddingService.examineSignUpEnd(request, message, channel);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("竞价截止- 处理完成");
        } catch (Exception e) {
            log.error("============消费失败,尝试消息补发再次消费!==============");
            log.error(e.getMessage());
            channel.basicRecover(false);
        }

    }

    /**
     * 处理采购询价单定时任务业务
     */
    @RabbitListener(queues = MessageConstants.PURCHASE_INQUIRY_DELAY_QUEUE)
    public void purchaseInquiryCfgUserReceiveDealy(OrderRbbitMqRequest request, Message message, Channel channel) throws IOException {
        log.info("处理采购询价单定时任务业务:接收时间:{},接受内容:{}", LocalDateTime.now(), request.toString());
        try {
            //使用处理业务
            ExamineQuotedPriceBatchRequest examineQuotedPriceBatch = new ExamineQuotedPriceBatchRequest();
            examineQuotedPriceBatch.setId(request.getId());
            purchaseInquiryService.examineQuotedPriceBatch(examineQuotedPriceBatch);
            //通知 MQ 消息已被接收,可以ACK(从队列中删除)了
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("============消费失败,尝试消息补发再次消费!==============");
            log.error(e.getMessage());
            //basicRecover方法是进行补发操作， 其中的参数如果为true是把消息退回到queue但是有可能被其它的consumer(集群)接收到，设置为false是只补发给当前的consumer
            //channel.basicRecover(false);
           channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }

    }






}